﻿using sqlsearcher.Forms;
using sqlsearcher.Models;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace sqlsearcher.Services
{
    public class JingLManager
    {
        private ListView lv;
        public JingLManager(ListView lv)
        {
            this.lv = lv;
        }
        public void AddItems(Dictionary<int, string> dics, int parentSysNo )
        {
            if (dics != null && dics.Count > 0)
            {
                var historys = new List<ListViewItem>();
                foreach (ListViewItem item in this.lv.Items)
                {
                    if (parentSysNo.ToString() == item.Tag.ToString())
                        historys.Add(item);
                }
                var group = new ListViewGroup();
                group.Name = Guid.NewGuid().ToString();
                group.Tag= Guid.NewGuid().ToString();
                group.Header = (parentSysNo==0)?"": EnumHelper.GetDescription<AppEnum.Jingl_0>(parentSysNo);
                foreach (var item in dics)
                {
                    AddItem(item.Value, item.Key.ToString(), group);
                }
                this.lv.Groups.Insert(0, group);
            }
        }
        public void LoadChildItems(int parentSysNo, Dictionary<int, string> childs)
        {
            if (childs == null)
            {
                switch (parentSysNo)
                {
                    case (int)AppEnum.Jingl_0.txt:
                        if (this.lv.SelectedItems.Count > 0)
                            TFac<TxtFrm>.Instance.txtMsg.Text = this.lv.SelectedItems[0].Text;
                        TFac<TxtFrm>.Instance.StartPosition = FormStartPosition.CenterParent;
                        TFac<TxtFrm>.Instance.ShowDialog();
                        break;
                    case (int)AppEnum.Jingl_0.commonsql:
                        childs = AppConst.sqls;
                        AddItems(childs, parentSysNo);
                        break;
                    case (int)AppEnum.Jingl_0.computerConfig:
                        childs = AppConst.computers;
                        AddItems(childs, parentSysNo);
                        break;
                    case (int)AppEnum.Jingl_0.C:
                        childs = AppConst.CExtends;
                        AddItems(childs, parentSysNo);
                        break;
                    default:
                        if (AppConst.computersAc.ContainsKey(parentSysNo))
                        {
                            if (AppConst.computersAc[parentSysNo] != null)
                                AppConst.computersAc[parentSysNo](null, null);
                        }
                        else if (AppConst.sqlsAc.ContainsKey(parentSysNo))
                        {
                            if (AppConst.sqlsAc[parentSysNo] != null)
                            {
                                var res = AppConst.sqlsAc[parentSysNo]();
                                var group = new ListViewGroup();
                                group.Name = Guid.NewGuid().ToString();
                                group.Tag = Guid.NewGuid().ToString();
                                group.Header = EnumHelper.GetDescription<AppEnum.Jingl_0>((int)AppEnum.Jingl_0.commonsql) ;
                                AddItem(res, ((int)AppEnum.Jingl_0.txt).ToString(), group);
                                this.lv.Groups.Insert(0, group);
                            }
                        }
                        else if (AppConst.CExtendsAc.ContainsKey(parentSysNo))
                        {
                            if (AppConst.CExtendsAc[parentSysNo] != null)
                            {
                                AppConst.CExtendsAc[parentSysNo](null, null);
                            }
                        }
                        else if (AppConst.CExtendsT3Ac.ContainsKey(parentSysNo))
                        {
                            if (AppConst.CExtendsT3Ac[parentSysNo] != null)
                            {
                                AppConst.CExtendsT3Ac[parentSysNo](null, null);
                            }
                        }
                        else if (AppConst.JinglAc.ContainsKey(parentSysNo))
                        {
                            if(AppConst.JinglAc[parentSysNo]!=null)
                                AppConst.JinglAc[parentSysNo](null, null);
                        }
                        break;
                }
            }
        }
        private void AddItem(string text, string value, ListViewGroup group)
        {
            var lvi = new ListViewItem();
            lvi.Text = text;
            lvi.Tag = value;
            lvi.Font = new System.Drawing.Font("宋体", 10, FontStyle.Underline);
            lvi.ForeColor = Color.Black;
            lvi.Group =group;
            group.Items.Insert(0, lvi);
            this.lv.Items.Insert(0, lvi);

        }
       
    }
}
